package com.gjt.stream;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Created with Intel IDEA.
 *
 * OutputStream(java.io) 输出流:方法 write,flush,close 接收
 *  write（int a）:a的范围0-255超255 将这个数的最第三位写入 其他三个字节忽略
 *  flush() :缓存中的数据大小不够发送，这个方法强制发送；应当在关闭流之前刷新流，防止流中有数据
 *  close(); 关闭流，如果之后再写入就会抛出Io异常；如果长时间一个流不关闭可能会泄露文件句柄，网络端等；可以在finally 中关闭
 *
 *
 *
 * @Auther: gjt
 * @Date: 2024/3/18 18:53
 */
public class OutStream {

    /**
     * 向文件中写入，并且一次只能写入一个字符
     */
    private static void getGenerateCharacter(OutputStream out) throws IOException {
        int firstPrintableCharacter = 33;
        int numberOfPrintableCharacters = 94;
        int numberOfCharactersPerLine = 72;
        int count = 0;

        int start = firstPrintableCharacter;
        while(count < 100){
            for(int i = start; i< start + numberOfCharactersPerLine; i++){
                out.write(((i-firstPrintableCharacter) % numberOfPrintableCharacters) + firstPrintableCharacter);
            }
            // 回车13 换行10
            out.write('\r');
            out.write('\n');
            start = ((start+1) - firstPrintableCharacter) % numberOfPrintableCharacters + firstPrintableCharacter;
            count++;
        }
    }

    /**
     * 向文件中写入，并且一次写入line[]数组 大小个字符（94）
     */
    private static void getGenerateCharacter1(OutputStream out) throws IOException {
        int firstPrintableCharacter = 33;
        int numberOfPrintableCharacters = 94;
        int numberOfCharactersPerLine = 72;
        int count = 0;
        int start = firstPrintableCharacter;

        // 加2是对应 回车和换行
        byte[] line = new byte[numberOfCharactersPerLine+2];

        while(count < 100){
            for (int i = start; i < start + numberOfCharactersPerLine; i++){
                line[i-start] = (byte) ((i-firstPrintableCharacter) % numberOfPrintableCharacters + firstPrintableCharacter);
            }
            line[72] = (byte) '\r';
            line[73] = (byte) '\n';

            // 一次写入74个字符   line[]当作缓存
            out.write(line);
            start = ((start+1)-firstPrintableCharacter) % numberOfPrintableCharacters + firstPrintableCharacter;
            count++;
        }

    }

    public static void main(String[] args) throws IOException {
        /**
         * note: 只要对象实现了callable接口都可以使用带资源的try;例外 javaMail Transport对象需要显示释放
         */
        try(OutputStream out = Files.newOutputStream(Paths.get("C:/Users/23152/Desktop/hello.txt"))){
            getGenerateCharacter(out);
        }catch (IOException e){
            e.printStackTrace();
        }


//        // 关闭流的模板
//        OutputStream out = null;
//        try{
//            //out = new FileOutputStream("C:/Users/23152/Desktop/hello.txt"); 等价下边的
//            out = Files.newOutputStream(Paths.get("C:/Users/23152/Desktop/hello.txt"));
//            getGenerateCharacter1(out);
//        }catch (IOException e){
//            System.err.println(e.getMessage());
//        }finally {
//            if(out != null){
//                try{
//                    out.close();
//                }catch (IOException ex){
//                    ex.printStackTrace();
//                }
//            }
//        }


    }
}
